﻿using System;
using System.Linq;
using System.Windows.Forms;
using Relocation.Forms;
using Relocation.Data;
using Relocation.Com.Tools;
using Relocation.Com;
using Relocation.Base;
using Relocation.UI;

namespace Relocation
{
	public partial class VillagesList : VillagesListHelp
	{		

		#region 构造函数
		public VillagesList()
			: base()
		{
			InitializeComponent();
			Init();
		}

		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="session">Session</param>
		public VillagesList(Session session)
			: base(session)
		{
			InitializeComponent();
			Init();
		}
		#endregion

		private void Init()
		{
			this.ObjectQuery = this.Session.Project.Villages.CreateSourceQuery();
			this.SortField = Village.GetPropName(t => t.updated);
			ControlTag tag = new ControlTag(new RoleInfo(Session.KEY_ROLE_ADMIN, Session.KEY_ROLE_OPERATOR));
			this.ButtonAdd.Tag = tag;
			this.ButtonDel.Tag = tag;
			this.ButtonEdit.Tag = tag;
		}

		/// <summary>
		/// 重写Refresh，刷新当前页面数据
		/// </summary>
		public override void Refresh()
		{
			base.Refresh();
			this.Reload();
		}


		/// <summary>
		/// 登记按钮
		/// </summary>		
		private void ButtonAdd_Click(object sender, EventArgs e)
		{
			try
			{
				using (VillagesWindow VillagesWindow = new VillagesWindow(this.Session))
				{
					if (VillagesWindow.ShowDialog().Equals(DialogResult.OK))
						this.Refresh();
				}
			} catch (Exception ex)
			{
				Log.Error(ex.GetInnerExceptionMessage());
				this.MessageBoxError("保存失败！");
			}
		}

		/// <summary>
		/// 修改按钮
		/// </summary>		
		private void ButtonEdit_Click(object sender, EventArgs e)
		{
			try
			{
				Village villages = this.GetSelectEntity();
				if (villages == null)
					return;
				using (VillagesWindow VillagesWindow = new VillagesWindow(this.Session, villages))
				{
					if (VillagesWindow.ShowDialog().Equals(DialogResult.OK))
						this.Refresh();
				}
			} catch (Exception ex)
			{
				Log.Error(ex.GetInnerExceptionMessage());
				this.MessageBoxError("保存失败！");
			}
		}

		/// <summary>
		/// 删除按钮
		/// </summary>		
		private void ButtonDelete_Click(object sender, EventArgs e)
		{
			try
			{
				Village villages = this.GetSelectEntity();
				if (villages == null)
					return;
				DialogResult dialogResult = MyMessagebox.Confirm("您确定要删除该单位及其合同文件吗？");
				if (!dialogResult.Equals(DialogResult.Yes))
					return;

				villages.Files.Load();
				while (villages.Files.Count > 0)
				{
					Relocation.Data.File files = villages.Files.First();
					string path = files.path;
					//先删除记录
					this.Session.DataModel.Delete(villages.Files.First());
					//再删除文件
					Tools.DeleteFile_Directory(path);
				}
				if (!villages.Areas.IsLoaded)
					villages.Areas.Load();
				while (villages.Areas.Count > 0)
				{
					Area area = villages.Areas.First();
					this.Session.DataModel.Delete(area);
				}
				if (!villages.Compensates.IsLoaded)
					villages.Compensates.Load();
				while (villages.Compensates.Count > 0)
				{
					Compensate ccompensate = villages.Compensates.First();
					this.Session.DataModel.Delete(ccompensate);
				}
				this.Session.DataModel.Delete(villages);
			} catch (Exception ex)
			{
				string errText = Tools.GetExceptionMessage(ex);
				Log.Error(errText);
				MyMessagebox.Error(errText);
			}
			this.Refresh();
		}

		protected override void DataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			try
			{
				Village Villages = this.GetSelectEntity();
				if (Villages == null)
					return;
				using (AreaCompensateWindow AreaCcompensateWindow = new AreaCompensateWindow(this.Session, Villages))
				{
					AreaCcompensateWindow.ShowDialog();
				}
			} catch (Exception ex)
			{
				Log.Error(ex.GetInnerExceptionMessage());
				this.MessageBoxError(ex.GetInnerExceptionMessage());
			}
		}

	}
}
